package com.red.wine.adapter;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.List;

import com.red.wine.R;
import com.red.wine.adapter.CityAdapter.Holder;
import com.red.wine.model.CityListInfo;
import com.red.wine.model.TasteBySql;
import com.red.wine.model.TasteInfo;
import com.red.wine.util.StringUtil;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

/**
 */
public class HisAdapter extends BaseAdapter{
	private Context context;
	private List<TasteBySql> datasource;
	public HisAdapter(Context context, List<TasteBySql> datasource) {
		this.context = context;
		this.datasource = datasource;
	}
	public int getCount() {
		if (datasource != null && datasource.size() > 0) {
			return datasource.size();
		} else {
			return 0;
		}
	}

	public void refresh(List<TasteBySql> datasource) {
		this.datasource = datasource;
		notifyDataSetChanged();
	}
	
	public Object getItem(int position) {
		return datasource.get(position);
	}

	public long getItemId(int position) {
		return position;
	}

	public View getView(int position, View convertView, ViewGroup parent) {
		Holder composites= null;  
		if(convertView == null ) {
           composites = new Holder();
           convertView = LayoutInflater.from(context).inflate(R.layout.his_item,null);
           composites.name = (TextView) convertView.findViewById(R.id.his_item_title);
           composites.content = (TextView) convertView.findViewById(R.id.his_item_com);
           composites.icon = (ImageView) convertView.findViewById(R.id.his_item_icon);
           convertView.setTag(composites);
       }else {//在convertView不为空时，Recycler根据旧的item根据数据进行置换，从而直接使用旧的item即可！
    	   composites =  (Holder) convertView.getTag();
       }
		if(!StringUtil.isEmptyOrNull(datasource.get(position).getThumbnail_64())){
//			File file = new File(datasource.get(position).getThumbnail_64());
			Bitmap bitmap = BitmapFactory.decodeFile(datasource.get(position).getThumbnail_64());
			composites.icon.setImageBitmap(bitmap);
		}
		if(!StringUtil.isEmptyOrNull(datasource.get(position).getTitle().toString())){
			composites.name.setText(datasource.get(position).getTitle().toString() + "");
		}else{
			composites.name.setText("");
		}
		if(!StringUtil.isEmptyOrNull(datasource.get(position).getContent().toString())){
			composites.content.setText(datasource.get(position).getContent().toString().subSequence(0, 25) + "...");
		}else{
			composites.content.setText("");
		}
       return convertView;
	}

	public class Holder {
		public TextView name;
		public TextView content;
		public ImageView icon;
	}
	
}
